#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>

#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <asm-i386/io.h>

#include <linux/mman.h>

unsigned long pagemem;
unsigned long *kmallocmem;
unsigned long *vmallocmem;

static int hello_init(void)
{ 
	printk(KERN_EMERG"Hello! This is the first test module!\n");

	pagemem = __get_free_page(GFP_KERNEL);
	if(!pagemem)
		goto fail3;
	printk(KERN_EMERG"<1>pagemem virt=0x%lx\n",&pagemem);
	printk(KERN_EMERG"<1>pagemem phys=0x%lx\n",virt_to_phys(&pagemem));

	//100 Bytes
	kmallocmem = (unsigned long*)kmalloc(100,GFP_KERNEL);
	if(!kmallocmem)
		goto fail2;
	printk(KERN_EMERG"<1>kmallocmem virt=0x%lx\n",kmallocmem);
	printk(KERN_EMERG"<1>kmallocmem phys=0x%lx\n",virt_to_phys(kmallocmem));

	//1000000 Bytes
	vmallocmem = (unsigned long*)vmalloc(1000000);
	if(!vmallocmem)
		goto fail1;
	printk(KERN_EMERG"<1>vmallocmem virt=0x%lx\n",vmallocmem);
	return 0;

fail1:
	kfree(kmallocmem);
fail2:
	free_page(pagemem);
fail3:
	return -1;
}
static void hello_exit(void)
{
	printk(KERN_EMERG"Module exit! Bye Bye!\n");
	free_page(pagemem);
	kfree(kmallocmem);
	vfree(vmallocmem);
	return;
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("GPL");
